package com.siwei.mes.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.ExperimentItemorder;
import com.siwei.mes.entity.tgl.TglItemorder;
import com.siwei.mes.mapper.ExperimentItemorderMapper;
import com.siwei.mes.service.ExperimentItemorderService;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账与小票关系(ExperimentItemorder)表服务实现类
 *
 * @author linzi
 * @since 2023-12-19 22:36:28
 */
@Service
public class ExperimentItemorderServiceImpl implements ExperimentItemorderService {
    @Resource
    private ExperimentItemorderMapper experimentItemorderMapper;

    @Override
    public Long create(ExperimentItemorder entity) {
        experimentItemorderMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ExperimentItemorder entity) {
        ExperimentItemorder experimentItemorder = experimentItemorderMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentItemorder == null, "试验台账与小票关系信息不存在");
        experimentItemorderMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentItemorder experimentItemorder = experimentItemorderMapper.selectByPrimaryKey(id);
        assertTrue(experimentItemorder == null, "试验台账与小票关系信息不存在");
        experimentItemorderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentItemorder> getPage(PageQuery<ExperimentItemorder, ExperimentItemorder> query) {
        ExperimentItemorder searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentItemorderMapper.getList(searchParam));
    }

    @Override
    public ExperimentItemorder loadById(Long id) {
        return experimentItemorderMapper.loadById(id);
    }

    @Override
    public List<ExperimentItemorder> getAll(ExperimentItemorder entity) {
        return experimentItemorderMapper.getList(entity);
    }

    @Override
    public List<TglItemorder> selectByExperimentId(Long experimentId) {
        List<TglItemorder> tglItemorders = experimentItemorderMapper.selectByExperimentId(experimentId);
        for (TglItemorder entity : tglItemorders) {
            try {
                if (StringUtils.isNotBlank(entity.getExtrainfo())) {
                    String entityExtrainfo = StringEscapeUtils.unescapeJava(entity.getExtrainfo());
                    JSONObject jsonObject = JSON.parseObject(entityExtrainfo);
                    entity.setFhy(jsonObject.getString("fhy"));
                }
            } catch (Exception e) {
                entity.setFhy("");
            }
        }
        return tglItemorders;
    }
}
